package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.KnowledgeFood;
import com.itheima.service.KnowledgeFoodService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 知识库-膳食库管理
 */
@RestController
@RequestMapping("/knowledgefood")
public class KnowledgeFoodController {

    @Reference
    private KnowledgeFoodService knowledgeFoodService;

    /** 批量导入膳食库数据
     * Excel文件上传，并解析文件内容保存到数据库
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            //读入excel文件，解析后返回
            List<String[]> excelList = POIUtils.readExcel(excelFile);
            //excel文件有内容，遍历文件内容将内容存入knowledgefood对象中
            if (excelFile != null && excelFile.getSize() > 0) {
                List<KnowledgeFood> knowledgeFoodList = new ArrayList<>();
                //遍历excelList
                for (String[] strings : excelList) {
                    //将excel文件内容存入knowledgefood对象
                    KnowledgeFood knowledgeFood = new KnowledgeFood(strings[0],strings[1],strings[2],strings[3],strings[4]);
                    //将ordersetting对象存入orderSettingList集合中
                    knowledgeFoodList.add(knowledgeFood);
                }
                //调用服务层批量添加方法
                knowledgeFoodService.addAll(knowledgeFoodList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //文件导入失败
            return new Result(false, MessageConstant.IMPORT_KNOWLEDGEFOOD_FAIL);
        }
        //文件导入成功
        return new Result(true, MessageConstant.IMPORT_KNOWLEDGEFOOD_SUCCESS);
    }



    /**
     * 新增膳食库数据
     * @param knowledgeFood
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody KnowledgeFood knowledgeFood) {
        try {
            //调用服务层添加方法
            knowledgeFoodService.add(knowledgeFood);
        } catch (Exception e) {
            e.printStackTrace();
            //新增膳食库数据失败
            return new Result(false, MessageConstant.ADD_KNOWLEDGEFOOD_FAIL);
        }
        //新增膳食库数据成功
        return new Result(true, MessageConstant.ADD_KNOWLEDGEFOOD_SUCCESS);
    }

    /**
     * 分页查询膳食库数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //获取前台传的参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //调用服务层分页查询方法
        return knowledgeFoodService.pageQuery(currentPage, pageSize, queryString);
    }

    /**
     * 编辑膳食数据
     * @param knowledgeFood
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody KnowledgeFood knowledgeFood){
        try {
            //调用服务层编辑方法
            knowledgeFoodService.edit(knowledgeFood);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑膳食数据失败
            return new Result(false, MessageConstant.EDIT_KNOWLEDGEFOOD_FAIL);
        }
        //编辑膳食数据成功
        return new Result(true, MessageConstant.EDIT_KNOWLEDGEFOOD_SUCCESS);
    }

    /**
     * 删除膳食数据
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            //调用服务层删除方法
            knowledgeFoodService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除膳食数据失败
            return new Result(false, MessageConstant.DELETE_KNOWLEDGEFOOD_FAIL);
        }
        //删除膳食数据成功
        return new Result(true, MessageConstant.DELETE_KNOWLEDGEFOOD_SUCCESS);
    }
}
